// pages/member/usage/list/index.js
import {
  request
} from '../../../../request';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    usageList:[],
    modalName: null
  },
  onShow: function () {
    this.initData();
  },
  initData() {
    request("/gym/usage/list", "GET").then(resopnse => {
      console.log(resopnse)
      this.setData({
        usageList: resopnse.rows
      });
    })
  },
  pageBack() {
    wx.navigateBack({
      delta: 1
    });
  }, // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  delete(e) {
    var that = this;
    var item = e.currentTarget.dataset['item'];
    wx.showModal({
      title: '提示',
      content: '是否删除当前记录',
      success(res) {
        if (res.confirm) {
          request("/gym/usage/" + item.usageId, "DELETE").then(res => {
            that.initData();
            if (res.code == "200") {
              wx.showToast({
                title: '删除成功',
                icon: '',
                duration: 2000,
              })
              
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000,
              })
            }
          })
          
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})